<?php

// Step 1: Load Nette Framework
// this allows Nette to load classes automatically so that
// you don't have to litter your code with 'require' statements
require_once LIBS_DIR . '/Nette/loader.php';

// Step 2: Enable Nette\Debug
// for better exception and error visualisation
Debug::enable();

$loader = new RobotLoader();
$loader->addDirectory(APP_DIR);
$loader->addDirectory(LIBS_DIR);
$loader->register();

// Step 3: Get the front controller
$application = Environment::getApplication();

Environment::loadConfig();

$router = $application->getRouter();

$system = SystemModel::getInstance();

DbModel::connect();

$user = Environment::getUser();

Environment::setVariable("options", $system->getOptions());
$session = Environment::getSession();

$sess_exp = $system->existsOption("sess_exp");
if(empty($sess_exp)){
	// defaultne nastavime 30 minut
	$system->setOptionVariable("sess_exp", 30);
}

$user->setExpiration($system->getOptionVariable("sess_exp")*60);


if (!$session->isStarted()) $session->start();

// LANGUAGES
$system->initLanguage();
if(isset($_POST["id_language_admin"])){
	$system->setLanguage($_POST["id_language_admin"]);
	Environment::getHttpResponse()->redirect(Environment::getHttpRequest()->getUri()->scriptPath."admin/");
}

$system->initLanguageWeb();
if(isset($_POST["id_language_web"])){
	$system->setLanguageWeb($_POST["id_language_web"]);
	Environment::getHttpResponse()->redirect(Environment::getHttpRequest()->getUri()->scriptPath);
}

$current_language = $system->getCurrentLanguage();
	
$lang = $current_language[0]["abbr"];
// nastavení jazyka pro potřeby gettext
/*setlocale(LC_MESSAGES, $lang);
putenv("LANG=$lang");
bindtextdomain("messages", "./locale/");
textdomain("messages");*/

	$menu = $system->getMenuForRouter();
	//Debug::consoleDump($menu, "router");
	// zajisti routovani k menu, pridava parametry zadane v modulu
	foreach ($menu as $item){
		
		$par = array(
			"presenter" => $item["presenter"],
			"action" => "default",
			"id_menu" => $item["id_menu"]
			);
		
		if(!empty($item["params"])) $params = explode(";",$item["params"]);
		else $params = array();
		$o = "";
		foreach($params as $p){
			$o .= "<$p>/";
			$par[$p] = NULL;
		}
		if($o=="/") $o = "";
		
		if($item["default"]) $abbr = "";
		else $abbr = $item["abbr"]."/";
		
		$router[] = new Route($abbr."{$item["name_seo_router"]}<id {$item["name_seo"]}>/".$o, $par);
		
		if($item["start"]==1) $defaultItem = $item;
	}
	
	// System pouze pro admina nebo superAdmina
	$router[] = new Route('admin/system/<action>/<id>/<ident>/', array(
	'presenter' => "System",
	'action' => 'default',
	'id' => NULL,
	'ident' => NULL
	));
	
	$menu = $system->getMenuForRouter(TRUE);
	foreach($menu as $item){
		$router[] = new Route("admin/<action>/<id {$item["name_seo"]}>/", array(
		"presenter" => "Admin".$item["presenter"],
		"id" => NULL,
		"action" => "default"
		));
	}
	
	
	// Default ROUTE
	if(!empty($defaultItem)){
	$router[] = new Route('', array(
	'presenter' => $defaultItem["presenter"],
	'action' => 'default',
	'id' => $defaultItem["name_seo"]
	));
	}
	// kdyz neni nastavena zakladni cesta, zobrazime chybu
	else{
	$router[] = new Route('', array(
	'presenter' => "Login",
	'action' => 'defaultNotSet',
	));
	}
	// Default ROUTE Admin
	$router[] = new Route('admin', array(
	'presenter' => "Web",
	'action' => 'default',
	));
	
	// cesta pro login
	$router[] = new Route('login/<action>', array(
	'presenter' => "Login",
	'action' => 'default',
	));
//Debug::consoleDump($router, "router");
// Step 4: Run the application!
//$application->onStartup[] = 'DbModel::connect';
$application->onShutdown[] = 'DbModel::disconnect';
$application->run();

?>
